﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
    #region Declaration
    string state, phone, strSubject, strTo, strFrom, strBody;
    email myEmail = new email();
    string sGuid, visitdate;
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        // Response.Write(Guid.NewGuid());
        PopulateTime();
    }
    private void PopulateTime()
    {
        ddVisitTime.Items.Add(" ");
        for (int i = 9; i < 13; i++)
        {

            if (i > 8 && i < 12)
                ddVisitTime.Items.Add(i.ToString() + " AM");
            if (i == 12)
                ddVisitTime.Items.Add(i.ToString() + " Noon");


        }
        for (int j = 1; j < 4; j++)
        {
            ddVisitTime.Items.Add(j.ToString() + " PM");

        }





    }
    protected void ddState_SelectedIndexChanged(object sender, EventArgs e)
    {
        state = ddState.SelectedValue.ToString();

    }
    private void setstate()
    {
        Session["Name"] = txtName.Text.Trim();
        Session["Add"] = txtAdd.Text.Trim();
        Session["City"] = txtCity.Text;
        Session["State"] = ddState.SelectedValue.ToString();
        Session["Zip"] = txtZip.Text;
        Session["Email"] = txtEmail.Text;
        if (txtArea.Text != "" && txtPrefix.Text != "" && txtSuffix.Text != "")
        {
            phone = txtArea.Text + '-' + txtPrefix.Text + '-' + txtSuffix.Text;
            Session["Phone"] = phone;
        }

        else
        {
            phone = "Not Provided";
            Session["Phone"] = phone;


        }
        if (txtOrg.Text != "")
        {
            Session["Org"] = txtOrg.Text.Trim();
        }
        else
        {
            txtOrg.Text = "Not Provided";
            Session["Org"] = txtOrg.Text.Trim();

        }





        if (txtStud.Text != "")
        {
            Session["Stud"] = txtStud.Text.Trim();
        }
        else
        {
            txtStud.Text = "Not Provided";
            Session["Stud"] = txtStud.Text.Trim();

        }

        if (txtAdult.Text != "")
        {
            Session["Adult"] = txtAdult.Text.Trim();
        }
        else
        {
            txtAdult.Text = "Not Provided";
            Session["Adult"] = txtAdult.Text.Trim();

        }

        Session["Agroup"] = ddAgeGrp.SelectedValue;
        if (ddAgeGrp.SelectedValue != "")
        {
            Session["Agroup"] = ddAgeGrp.SelectedValue;
        }
        else
        {

            Session["Agroup"] = "Not Given";

        }
        Session["txtDate"] = txtday.Text;
        Session["txtTime"] = ddVisitTime.SelectedValue;
        Session["rbLunch"] = rbLunch.SelectedValue.ToString();

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {

        if (Page.IsValid)
        {
            setstate();

            visitdate = txtday.Text + " @ " + ddVisitTime.SelectedValue;

            if (Convert.ToString(Session["appGuid"]) != "")
                sGuid = Session["appGuid"].ToString();
            else
            {
                Session["appGuid"] = Guid.NewGuid();
                sGuid = Session["appGuid"].ToString();
            }

            SaveGroupVisit();

            sendEmail();
            Response.Redirect("~/thanks.aspx");
        }
    }

    private void SaveGroupVisit()
    {
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["prod"].ConnectionString))
        {
            SqlCommand cmd = new SqlCommand("CUS_sp_Insert_GroupVisit", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@app_guid", SqlDbType.VarChar).Value = sGuid;

            string name = txtName.Text;
            int position = name.IndexOf(@" ");
            if (position > 0)
            {

                cmd.Parameters.Add("@firstName", SqlDbType.VarChar).Value = name.Substring(0, position);
                cmd.Parameters.Add("@lastName", SqlDbType.VarChar).Value = name.Substring(position + 1);
            }
            else
            {
                cmd.Parameters.Add("@firstName", SqlDbType.VarChar).Value = txtName.Text;
                cmd.Parameters.Add("@lastName", SqlDbType.VarChar).Value = txtName.Text;
            }

            cmd.Parameters.Add("@Address", SqlDbType.VarChar).Value = txtAdd.Text;

            cmd.Parameters.Add("@State", SqlDbType.VarChar).Value = ddState.SelectedValue.ToString();
            cmd.Parameters.Add("@City", SqlDbType.VarChar).Value = txtCity.Text;
            cmd.Parameters.Add("@Zipcode", SqlDbType.VarChar).Value = txtZip.Text;

            cmd.Parameters.Add("@HomePhone", SqlDbType.VarChar).Value = phone;

            cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = txtEmail.Text;
            cmd.Parameters.Add("@School", SqlDbType.VarChar).Value = txtOrg.Text;
            cmd.Parameters.Add("@students", SqlDbType.VarChar).Value = txtStud.Text;

            cmd.Parameters.Add("@AgeGrp", SqlDbType.VarChar).Value = ddAgeGrp.SelectedValue.ToString();

            cmd.Parameters.Add("@Adult", SqlDbType.VarChar).Value = txtAdult.Text;
            cmd.Parameters.Add("@Lunch", SqlDbType.VarChar).Value = rbLunch.SelectedValue.ToString();
            cmd.Parameters.Add("@VisitDateTime", SqlDbType.VarChar).Value = visitdate;
            try
            {
                conn.Open();
                cmd.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                string err = "SQL Exception Caught " + ex.Message.ToString();
                //return err;

            }
            finally
            {

                conn.Close();
            }



        }
    }

    private void sendEmail()
    {
        strTo = "Admission@Avila.edu ";
        strSubject = "Campus Group Visit Request Submitted On " + DateTime.Now;// +Label1.Text;
        strFrom = txtEmail.Text.Trim();
        strBody = "";
        strBody += " <table style='width:612px;' > <tr valign='top' align='left' ><th align='left' >Name</th> ";
        strBody += "<td align='left'> " + Session["Name"].ToString() + "</td><th align=left>Email</th> <td align=left >" + Session["Email"].ToString() + "</td></tr>";

        strBody += "<tr valign='top' align='left' ><th align='left' >Address</th> ";
        strBody += "<td align='left'> " + Session["Add"].ToString() + "</td><th align=left>City</th> <td align=left >" + Session["City"].ToString() + "</td></tr>";


        strBody += "<tr valign='top' align='left' ><th align='left' >State</th> ";
        strBody += "<td align='left'> " + Session["State"].ToString() + "</td><th align=left>Zip Code</th> <td align=left >" + Session["Zip"].ToString() + "</td></tr>";


        strBody += "<tr valign='top' align='left' ><th align='left' >Phone</th> ";
        strBody += "<td align='left'> " + Session["Phone"].ToString() + "</td><th align=left>Organization</th> <td align=left >" + Session["Org"].ToString() + "</td></tr>";

        strBody += "<tr valign='top' align='left' ><th align='left' >Visit Date</th> ";
        strBody += "<td align='left'> " + Session["txtDate"].ToString() + "</td><th align=left>Visit Time</th> <td align=left >" + Session["txtTime"].ToString() + "</td></tr>";

        strBody += " </table><table style='width:612px;' > <tr valign='top' align='left' ><th align='left' colspan=4 >How many students and Adult ( there must be 1 adult per 10 kids)</th></tr> ";

        strBody += "<tr valign='top' align='left' ><th align='left' >No of Students</th> ";
        strBody += "<td align='left'> " + Session["Stud"].ToString() + "</td><th align=left>Age Group</th> <td align=left >" + Session["Agroup"].ToString() + "</td></tr>";

        strBody += "<tr valign='top' align='left' ><th align='left' >No of Adult</th> ";
        strBody += "<td align='left'> " + Session["Adult"].ToString() + "</td></tr>";


        strBody += " </table><table style='width:612px;' > <tr valign='top' align='left' ><th align='left' colspan=4 >Will the group be eating lunch in the cafeteria? " + Session["rbLunch"].ToString() + "</th></tr></table>";


        // myEmail.sendMail(strBody, strFrom, strTo, "Heather.McDaniel@avila.edu ", strSubject);
        //myEmail.sendMail(strBody, strFrom, strTo, "Mallika.Ramaswamy@avila.edu ", strSubject);

    }
}